//
//  Unit.m
//  jiajiao
//
//  Created by apple on 16/3/15.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "Unit.h"
@implementation Unit
+ (BOOL)isMobileNumber:(NSString *)mobileNum
{
    BOOL isMobile;
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[0-35-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189
     22         */
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        isMobile = YES;
    }else{
        isMobile =  NO;
    }
    return isMobile;
}
//计算文字高度
+ (CGSize)getSizeWithStrNo:(NSString*)str width:(float)width font:(float)font
{
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:font]};
    CGSize temSize = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                       options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                    attributes:attribute context:nil].size;
    return temSize;
}
+ (CGSize)getSizeWithStrNo:(NSString*)str hight:(float)hight font:(float)font
{
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:font]};
    CGSize temSize = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, hight)
                                       options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                    attributes:attribute context:nil].size;
    return temSize;
}

//处理图片高度
+(CGSize)getHightWith:(UIImage *)image Width:(float)width
{
    CGSize size;
    
    if (image.size.width>width) {
        float scr = width/image.size.width;
         size= CGSizeMake(width, image.size.height*scr);
        return size;
    }else if (image.size.width<=width)
    {
        size=image.size;
        return size;
    }
    return size;
}
//返回可拉伸图片；
+ (UIImage *)resizableImage:(NSString *)name
{
    UIImage *normal = [UIImage imageNamed:name];
    CGFloat w = normal.size.width * 0.5;
    CGFloat h = normal.size.height * 0.5;
    return [normal resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w)];
}
+(CGFloat)gaveMeArray:(NSArray *)array
{
    NSMutableArray *mutablearray = [NSMutableArray array];
    [array enumerateObjectsUsingBlock:^(NSValue *obj, NSUInteger idx, BOOL * _Nonnull stop) {
      CGRect fram = [(NSValue *)obj CGRectValue];
        CGFloat Width = fram.size.width;
        NSNumber *number = [NSNumber numberWithFloat:Width];
        [mutablearray addObject:number];
    }];
    NSArray *array1 = mutablearray;
    NSArray *array2 =[array1 sortedArrayUsingSelector:@selector(compare:)];
    NSNumber *big = array2.lastObject;
    CGFloat bigfloat = [big floatValue];
    return bigfloat;
}
+(NSString *)latitude:(CLLocation *)location
{
    NSString *str =[NSString stringWithFormat:@"%f",location.coordinate.latitude];
    return str;
}
+(NSString *)longtitude:(CLLocation *)location
{
    NSString *str =[NSString stringWithFormat:@"%f",location.coordinate.longitude];
    return str;
}
+ (UIImage *)buttonImageFromColor:(UIColor *)color
{
    
    CGRect rect = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 64);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
+(NSString *)getNewTime
{
    NSDate *  senddate=[NSDate date];
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    
    [dateformatter setDateFormat:@"YYYY-MM-dd"];
    
    NSString *  locationString=[dateformatter stringFromDate:senddate];
    
    return locationString;

}
+(NSInteger)disstanceFromNow:(NSString *)time
{
    NSDate *  senddate=[NSDate date];
    
    
    NSDateFormatter *formater = [[NSDateFormatter alloc]init];
    [formater setDateFormat:@"YYYY-MM-dd"];
    NSDate *data = [formater dateFromString:time];
    
    NSTimeInterval ti=[data timeIntervalSinceDate:senddate];
    
    int days=((int)ti)/(3600*24);
    
    return days;

}

+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate
{
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
    
}
+(NSDate *)getDateFromString:(NSString *)str
{
    NSDateFormatter *fromt = [[NSDateFormatter alloc]init];
    [fromt setDateFormat:@"YYYY-MM-dd"];
    NSDate*dat = [fromt dateFromString:str];
    return dat;

}
+(NSString *)gavewmemiao:(NSInteger)miao
{
    NSString *str;
    
    if (miao/60>1&&miao/60<60) {
        str= [NSString stringWithFormat:@"%ld分",miao/60];
        
    }else if (miao/60>=60)
    {
        
        NSInteger fenzhong = miao/60;
        NSInteger hour = fenzhong/60;
        NSInteger losfenzhong = fenzhong%60;
        str = [NSString stringWithFormat:@"%ld时%ld分",hour,losfenzhong];
    }else
    {
    return str = @"0";
    }
    
    return str;
}
+(NSString *)gavemeMetter:(NSInteger)meter
{
    if (meter<=1000) {
        return [NSString stringWithFormat:@"%ld米",meter];
    }else
    {
        
//        CGFloat km = [meter doubleValue];
        
        CGFloat km = (CGFloat)meter/1000;
        
        return [NSString stringWithFormat:@"%.1f千米",km];
    }


}
@end
